import Taro from "@tarojs/taro";

export const getImageURL = (url) => {
  return `${process.env.TARO_APP_ASSETS_BASE}${url}`;
};

// 判断矩形是否相交
export const isPointInRectangle = (point, rectangle) => {
  const { x: x_p, y: y_p } = point;
  const { x, y, width, height } = rectangle;

  const x_min = x;
  const y_min = y;
  const x_max = x + width;
  const y_max = y + height;

  return x_min <= x_p && x_p <= x_max && y_min <= y_p && y_p <= y_max;
};

export const PromiseWith = () => {
  const result = {};
  result.promise = new Promise((resolve, reject) => {
    result.resolve = resolve;
    result.reject = reject;
  });
  return result;
};

export const Login = () => {
  const { promise, resolve, reject } = PromiseWith();
  Taro.login({
    success(res) {
      const { code, errMsg } = res;
      if (errMsg !== "login:ok") return reject(new Error("登录失败"));
      resolve(code);
    },
    fail() {
      reject(new Error("登录失败"));
    },
  });
  return promise;
};
